package com.sz10.p2p.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.sz10.p2p.model.LoanInfo;
import com.sz10.p2p.service.LoanInfoService;
import com.sz10.p2p.vo.PaginationVO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName:LoanInfoController
 * Package:com.sz10.p2p.web
 * Description: 描述信息
 *
 * @date:2021/6/27 22:50
 * @author: lolia
 */
@Controller
public class LoanInfoController {

    @Reference(interfaceClass = LoanInfoService.class, version = "1.0.0", timeout = 15000, check = false)
    private LoanInfoService loanInfoService;


    @RequestMapping("/loan/loan")
    public String toLoanInfoList(Model model,
                                 @RequestParam(value = "ptype", required = false) Integer ptype,
                                 @RequestParam(value = "currentpage", defaultValue = "1") Integer currentPage) {

        //需要的参数（productType(传进来就加，不传就不加)、每页查询的条数、当前页）
        Map<String, Object> paramMap = new HashMap<>();
        if (ObjectUtils.allNotNull(ptype)) {
            paramMap.put("productType", ptype);
        }
        int pageSize = 9;
        paramMap.put("pageSize", pageSize);
        paramMap.put("currentPage", (currentPage - 1) * pageSize);

        PaginationVO<LoanInfo> pageList = loanInfoService.queryLoanInfoListByPage(paramMap);
        //需要返回的数据List<LoanInfo> + Total 总条数
        //valueObject 值对象可以封装现有实体类不匹配的属性
        //1.List<LoanInfo>
        model.addAttribute("loanInfoList", pageList.getDataList());
        //2.总记录数
        model.addAttribute("total", pageList.getTotal());
        //3.计算总页数
        long totalpage = pageList.getTotal() / pageSize;

        int mod = (int) (pageList.getTotal() % pageSize);
        if (mod != 0) {
            totalpage = totalpage + 1;
        }

        model.addAttribute("totalPage", totalpage);
        model.addAttribute("currentPage",currentPage);

        //TODO 投资排行榜！！

        //TODO 跳转到产品的分页列表

        return "loan";
    }
}
